package com.vhall.component.dao.gift;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.vhall.component.entity.gift.dto.GiftRoomStatDTO;
import com.vhall.component.entity.gift.entity.GiftRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

@Mapper
@Repository
public interface GiftRecordMapper extends BaseMapper<GiftRecord> {

    @Select("SELECT COUNT(t.gift_user_id)"
            + "   FROM (SELECT gift_user_id FROM gift_record"
            + "       WHERE app_id=#{appId} AND receiver_id=#{receiverId} AND source_id=#{sourceId} AND bu=#{bu} and deleted=0 and pay_status=1"
            + "   GROUP BY gift_user_id"
            + ") t"
    )
    Integer listRankCounter(@Param("appId") String appId, @Param("receiverId") String receiverId, @Param("sourceId") String sourceId, @Param("bu") Integer bu);

    /**
     * 统计房间下表情赠送信息数据
     *
     * @param roomId     PAAS房间id
     * @param startTime  开始时间
     * @param endTime    结束时间(不包括)
     * @param startIndex 分页起始位置
     * @param pageSize   分页单页大小
     * @return 统计表情信息数据
     */
    @Select(" select gr.name,sum(gr.numbers)total," +
            " count(DISTINCT gr.gift_user_id)sender_number,sum(gr.numbers*gr.price)total_price," +
            " gr.gift_id\n" +
            " from gift_record gr\n" +
            " where gr.room_id=#{roomId} \n" +
            " and gr.created_at BETWEEN #{startTime} and #{endTime}\n" +
            " group by gr.name,gr.gift_id\n" +
            " order by gr.gift_id \n" +
            " limit #{startIndex},#{pageSize}")
    List<GiftRoomStatDTO> statRoomDataList(@Param("roomId") String roomId, @Param("startTime") LocalDate startTime
            , @Param("endTime") LocalDate endTime, @Param("startIndex") Integer startIndex, @Param("pageSize") Integer pageSize);

    /**
     * 统计房间下表情赠送统计信息条数
     *
     * @param roomId    房间id
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 统计信息条数
     */
    @Select(" select count(t.gift_id)num from(select gr.gift_id " +
            " from gift_record gr\n" +
            " where gr.room_id=#{roomId} \n" +
            " and gr.created_at BETWEEN #{startTime} and #{endTime}\n" +
            " group by gr.name,gr.gift_id) t")
    Integer countStatGiftRoom(@Param("roomId") String roomId, @Param("startTime") LocalDate startTime
            , @Param("endTime") LocalDate endTime);
}
